<template>
  <TableView
    ref="tableView"
    :table-attrs="{
      api: getWorkItemAssetPage,
      payloadStyle: 'body',
      pageSize: 10,
      descs: ['number'],
      dicOptions: [
          { prop: 'status', dicType: 'inventory_task_list_status' },
                      { prop: 'assetStatus', dicType: 'asset_status' }
      ]
  }"
    :search-model="searchForm"
    :delete-api="batchDelete"
    :toolbar-attrs="{ buttons: ['slot', { del: true, create: false }] }">
    <template #toolbar-button="{ selectedRows }">
      <ToolbarButton type="primary" icon="el-icon-zoom-in" size="small" @click.stop="visible = true">选择资产</ToolbarButton>
      <ToolbarButton type="primary" icon="el-icon-check" size="small" @click.stop="updateWorkItemAssetStatus(1)">存在</ToolbarButton>
      <ToolbarButton type="primary" icon="el-icon-minus" size="small" @click.stop="updateWorkItemAssetStatus(2)">不存在</ToolbarButton>
      <ToolbarButton type="primary" icon="el-icon-document-checked" size="small" @click.stop="updateWorkItemAssetStatus(3)">盘盈</ToolbarButton>
      <ToolbarButton type="primary" icon="el-icon-data-board" size="small" @click.stop="updateWorkItemAssetStatus(4)">盘亏</ToolbarButton>
      <ToolbarButton type="primary" icon="el-icon-folder-add" size="small" @click.stop="showDescription">添加备注</ToolbarButton>
    </template>
    <template #columns>
      <TableColumn label="盘点状态" prop="status" width="70"/>
      <TableColumn label="资产状态" prop="assetStatus" width="70"/>
      <TableColumn label="资产名称" prop="name"/>
      <TableColumn label="资产编码" prop="number"/>
      <TableColumn label="资产类型" prop="ctiName"/>
      <TableColumn label="规格型号" prop="model"/>
      <TableColumn label="计量单位" prop="unit"/>
      <TableColumn label="金额" prop="amount"/>
      <TableColumn label="使用人" prop="useName"/>
      <TableColumn label="存放地点" prop="location"/>
    </template>
    <template #search-form>
      <FormSelect v-model="searchForm.status" dict-type="inventory_task_list_status" label="盘点状态" prop="status" style="width: 220px;"  />
      <FormSelect v-model="searchForm.assetStatus" dict-type="asset_status" label="资产状态" prop="assetStatus" style="width: 220px;"  />
      <FormInput v-model="searchForm.name" label="资产名称" prop="name"  style="width: 220px;" />
      <FormInput v-model="searchForm.number" label="资产编码"  prop="number" style="width: 220px;" />
      <FormInput v-model="searchForm.ctiName" label="资产类型"  prop="ctiName" style="width: 220px;" />
      <FormInput v-model="searchForm.model" label="规格型号"  prop="model" style="width: 220px;" />
      <FormInput v-model="searchForm.unit" label="计量单位"  prop="unit" style="width: 220px;" />
    </template>
    <InventoryTaskAssetSelect ref="inventoryTaskAssetSelect" :visible.sync="visible" :inventory-task-id="inventoryTaskId" :work-item-id="workItemId" @confirm="getInventoryTaskAsset"/>

    <ElementDialog ref="dialog" embed height="30vh" width="60%" show-full-screen :visible="visibleDescription" title="添加意见">
      <FormTextarea v-model="description" rows="12" prop="description" label="盘点说明" max-length="2000" />
      <template #footer>
        <ElButton type="primary" @click="visibleDescription = false">取 消</ElButton>
        <ElButton type="primary" @click="addWorkItemDescription">确定</ElButton>
      </template>
    </ElementDialog>
  </TableView>
</template>

<script>
import {mapState} from 'vuex'
import {services} from '@/config'
import {getWorkItemAssetPage, batchDelete, batchSaveOrUpdate} from '@/api/workItem/workItemList'
import InventoryTaskAssetSelect from '@/module/inventory/InventoryTaskAssetSelect'

export default {
  name: 'WorkItemListAsset',
  components:{
    InventoryTaskAssetSelect
  },
  props: {
    inventoryTaskId: {
      type: String,
      default: '-1'
    },
    workItemId: {
      type: String,
      default: '-1'
    },
    workItemStatus: {
      type: String,
      default: '-1'
    }
  },
  data() {
    return {
      searchForm: {
        id: '',
        parentId: this.workItemId,
        sourceId: '',
        status: '',
        assetStatus: '',
        name: '',
        number: '',
        ctiName: '',
        model: '',
        unit: '',
        description: '',
        sourceType: ''
      },
      description: '',
      visibleDescription: false,
      visible: false
    }
  },
  computed: {
    services() {
      return services
    },
    ...mapState('user', ['permissions'])
  },
  methods: {
    getWorkItemAssetPage,
    batchDelete,
    getInventoryTaskAsset(rows){
      this.addWorkItemList(rows)
    },
    async addWorkItemDescription() {
      if (this.$isBlank(this.description)) {
        this.$message.warning('请输入盘点备注')
        return
      }
      const selectedRows = this.$refs.tableView.selectedRows
      const data = []
      selectedRows.forEach(row => {
        data.push({
          id: row.id,
          status: row.$status,
          parentId: row.parentId,
          sourceId: row.sourceId,
          sourceType: row.sourceType,
          description: this.description
        })
      })
      const res = await batchSaveOrUpdate(data)
      if (res.data.data) {
        this.$message.success('添加备注成功')
        await this.$refs.tableView.refreshTable()
      } else {
        this.$message.success('添加备注失败！')
      }
      this.visibleDescription = false
    },
    async updateWorkItemAssetStatus(status) {
      const selectedRows = this.$refs.tableView.selectedRows
      if (selectedRows.length === 0) {
        this.$message.warning('请选择数据')
        return
      }
      const data = []
      selectedRows.forEach(row => {
        data.push({
          id: row.id,
          status,
          parentId: row.parentId,
          sourceId: row.sourceId,
          sourceType: row.sourceType,
          description: row.description
        })
      })
      const res = await batchSaveOrUpdate(data)
      if (res.data.data) {
        this.$message.success('修改成功')
        await this.$refs.tableView.refreshTable()
      } else {
        this.$message.success('修改失败！')
      }
    },
    async addWorkItemList(rows) {
      const workItemList = [];
      rows.forEach(row => {
        workItemList.push({
          id: null,
          parentId: this.workItemId,
          sourceId: row.assetId,
          sourceType: "com.fuyao.cloud.asset.entity.AssetCard"
        })
      })
      const res = await batchSaveOrUpdate(workItemList)
      if(res.data.data){
        this.$message.success('关联成功')
        await this.$refs.tableView.refreshTable()
      }else{
        this.$message.success('关联失败！')
      }
    },
    showDescription(){
      const selectedRows = this.$refs.tableView.selectedRows
      if (selectedRows.length === 0) {
        this.$message.warning('请选择数据')
        return
      }
      this.visibleDescription = true
    },
    showBtn(){
      return this.workItemStatus !== '0'
    }
  }
}
</script>